<?php

namespace Ticket\Form;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use \Zend\Form\Form;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;

class TicketFormFactory implements FactoryInterface {

    public function createService(ServiceLocatorInterface $serviceLocator) {
        $form = new Form('ticket');
        $form->setAttribute('method', 'post');

        $form->add(array(
            'name' => 'id',
            'type' => 'hidden',
        ));
        $form->add(array(
            'name' => 'title',
            'type' => 'text',
            'options' => array(
                'label' => 'Tytuł',
            ),
        ));
        $categoryRepository = $serviceLocator->get('Ticket\Repository\CategoryRepository');
        $categories = $categoryRepository->getCategories();
        $form->add(array(
            'name' => 'category',
            'type' => 'select',
            'options' => array(
                'label' => 'Category',
                'empty_option' => 'Choose a category',
                'value_options' => array_combine($categories, $categories),
            ),
        ));
        $form->add(array(
            'name' => 'description',
            'type' => 'textarea',
            'options' => array(
                'label' => 'Opis',
            ),
        ));
        
        $form->add(array(
            'name' => 'submit',
            'type' => 'submit',
            'attributes' => array(
                'value' => 'Go',
                'id' => 'submitbutton',
            ),
        ));

        $inputFilter = $this->createFilter($serviceLocator);
        $form->setInputFilter($inputFilter);
        
        $form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods(false));

        return $form;
    }
    
    public function createFilter(ServiceLocatorInterface $serviceLocator) {
        $inputFilter = new InputFilter();
        $factory = new InputFactory();

        $inputFilter->add($factory->createInput(array(
                    'name' => 'id',
                    'required' => true,
                    'filters' => array(
                        array('name' => 'Int'),
                    ),
        )));
        
        $inputFilter->add($factory->createInput(array(
                    'name' => 'title',
                    'required' => true,
                    'filters' => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name' => 'StringLength',
                            'options' => array(
                                'encoding' => 'UTF-8',
                                'min' => 1,
                                'max' => 255,
                            ),
                        ),
                    ),
        )));

        $inputFilter->add($factory->createInput(array(
                    'name' => 'category',
                    'required' => true,
                    'filters' => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name' => 'StringLength',
                            'options' => array(
                                'encoding' => 'UTF-8',
                                'min' => 1,
                                'max' => 255,
                            ),
                        ),
                    ),
        )));

        $inputFilter->add($factory->createInput(array(
                    'name' => 'description',
                    'required' => true,
                    'filters' => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name' => 'StringLength',
                            'options' => array(
                                'encoding' => 'UTF-8',
                                'min' => 1,
                                'max' => 5000,
                            ),
                        ),
                    ),
        )));



        return $inputFilter;
    }

}